const _form = this.getApiPage().retrievePlugin('base-forms');
const $form = this.pageObj.getApiWidgetByAliasName('form_14');
const { id, list } = _form.getFormData('form_14') || {};
const modals = this.pageObj.getApiWidgetsByClassName('optss-cruise');
const edit_list = this.pageObj.getApiWidgetByAliasName('edit_list');
console.log('save', edit_list.widgetObj._option);
// const noData = this.pageObj.getApiWidgetByAliasName('noData');
// noData.hideWidget()
const variables = this.getApiPage().getVariables();
const proxy = variables.find(item => item.name === 'environmentType')?.value || '';

function basePost(url, body, header = {}) {
  const headers = { "Content-Type": "application/json", ...header };
  const option = { method: 'POST', headers, body: JSON.stringify(body) };
  return fetch(url, option).then(res => res.json());
}

if (Array.isArray(edit_list.widgetObj._option.dataSource) && edit_list.widgetObj._option.dataSource.length > 0) {
  const data = edit_list.widgetObj._option.dataSource.map(v => ({ ...v, presetNum: +v.presetNum }));
  const params = {
    equipmentId: id,
    cruisePointList: data,
    cruiseRoute: list
  };
  const headers = { "Accept": "application/json;charset=UTF-8" };
  basePost(`/${proxy}IotDdevice/project-business-iot-service/hikvision/sdk/modifyCruise`, params, headers)
    .then(({ success }) => {
      if (success) {
        modals.forEach(v => v.hideWidget());

        basePost(
          `/${proxy}IotDdevice/project-business-iot-service/hikvision/sdk/queryCruises`,
          { equipmentId: id }
        ).then(({ success, result }) => {
          if (success) {
            console.log('_form', _form);
            const value = result.map(v => ({
              ...v,
              name: v.cruiseRoute + '-巡航路径' + v.cruiseRoute,
              value: v.cruiseRoute
            }));
            this.pageObj.setVariables([{ name: 'route_list', value }])
          }
        })
      }else {
        window.layer.msg(json.resultMessage)
      }
    })
} else {
  noData.hideWidget();
  modals.forEach(v => v.hideWidget())
}